package com.atguigu.tingshu.user.login;

import org.bouncycastle.pqc.crypto.newhope.NHSecretKeyProcessor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
public class LoginClient implements ApplicationContextAware {

    private  final Map<Integer,ILogin> MAP = new ConcurrentHashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(LoginBean.class);

        Collection<Object> beans = beansWithAnnotation.values();
        beans.forEach(bean->{
            LoginBean loginBean = bean.getClass().getAnnotation(LoginBean.class);
            LoginType loginType = loginBean.value();
            MAP.put(loginType.type,(ILogin) bean);

        });


    }

    public Map<String,Object> login(int type,LoginAccount loginAccount){
       return MAP.get(type).login(loginAccount);
    }


}
